window.onload = function() {
    var form = document.querySelector('form')
    var username = document.querySelector('[name="username"]')
    var password = document.querySelector('[name="password"]')
    var repass = document.querySelector('[name="repass"]')
    var email = document.querySelector('[name="email"]')
    var tel = document.querySelector('[name="tel"]')
    form.onsubmit = function() {
        var reg = /^[a-zA-Z][a-zA-Z0-9]{4,16}$/
        if(!reg.test(username.value)) {
            popup.msg('用户名字母开头，字母数字组成4~16位！')
            return false
        }
        if(password.value.length < 6 || password.value.length > 12) {
            popup.msg('密码长度6~12！')
            return false
        }
        if(password.value != repass.value) {
            popup.msg('两次密码输入不一致！')
            return false
        }
        var reg = /(^[1-9]\d{4,9}@qq\.com$)|(^[a-zA-Z]\w{5,17}@(((126|163)\.com)|(yeah\.net))$)/
        if(!reg.test(email.value)) {
            popup.msg('邮箱不正确！')
            return false
        }
        var reg = /^1[345789]\d{9}$/
        if(!reg.test(tel.value)) {
            popup.msg('手机号不正确！')
            return false
        }
        request({
            method: 'post',
            url: '/front/api/users/register',
            data: {
                username: username.value,
                password: password.value,
                email: email.value,
                tel: tel.value
            }
        }).then(res => {
            var {error_code, msg} = res
            if(error_code === 0) {
                popup.msg(msg, () => {
                    location.href = '/front/login.html'
                })
            } else {
                popup.msg(msg)
            }
        })
        return false
    }
}